global data_folder "W:\intimate\data"
global log_folder "W:\intimate\dofiles\logs"
global result_folder "W:\intimate\results_revision"


local var_list ptoim1 tyotu ika ututku sukup ututku seutukunta16 tyopv toimiala 
forvalues i = 2000/2016 {
use `var_list' using "D:\ready-made\FLEED_TOTAL\2016\fleed_kokonais_`i'.dta", clear

destring ututku, replace 
g level = floor(ututku/100000)
replace level= 0 if missing(ututku)

gen educ=1 if level<3
replace educ=2 if leve>=3 & level<=5
replace educ=3 if level>=6

	
g male = (sukup=="1")
keep if ika>=20
g age = floor(ika/5)

g day_wage = tyotu/tyopv

destring ptoim1, replace
replace ptoim1 = (ptoim1==11)

*INDUSTRY AND OCCUPATION FOR EWCS DATA
	gen nace_2=real(substr(toimiala,1,2))

	loc vars "nace"
	foreach var in `vars' {
		g `var'=.
		replace `var'=1 if `var'_2==1 | `var'_2==2 | `var'_2==5
		replace `var'=2 if `var'_2>=10 & `var'_2<=37
		replace `var'=3 if `var'_2==40 | `var'_2==41
		replace `var'=4 if `var'_2==45  
		replace `var'=5 if `var'_2>=50 & `var'_2<=52
		replace `var'=6 if `var'_2==55  

	}

	drop nace_2 
	label var nace "Industry"

g emp = ptoim1
gen ann_earn= tyotu
*keep if tyotu~=.

g obs = 1 
collapse (mean) ptoim1 tyotu (sum) obs emp ann_earn, by(educ male age seutukunta16)

tempfile extra`i'
save `extra`i'', replace 

}

clear
gen year=.  
forvalues i = 2000/2016 {
    append using `extra`i''
	replace year=`i' if year==. 
} 


sum ptoim1 if male==0,d 
save "$data_folder\munic_edu_age_gender_oo", replace 

use "W:\intimate\data\dvonly_all.dta", clear 

duplicates report shnro ao_shnro year_event case_id
duplicates drop shnro ao_shnro year_event case_id, force 

rename shnro suspect_shnro 
rename ao_shnro shnro 

local var_list ututku sukup seutukunta16
forvalues i = 2006/2016 {
    preserve 
		keep if year_event == `i'
		
		merge m:1 shnro using "D:\ready-made\FLEED_TOTAL\2016\fleed_kokonais_`i'.dta", ///
			keepusing(`var_list')
		drop if _merge ==2 
		drop _merge 
	
	
		
		tempfile extra`i'
		save `extra`i''
		
	restore
}



preserve 
use `extra2006', clear
forvalues i = 2007/2016 {
    append using `extra`i''
}

// collapse the data into educ year and region cell 
rename year_event year 
gen dv=1 

destring ututku, replace 
g educ = floor(ututku/100000)
replace educ = 0 if missing(ututku)

collapse (sum) dv, by(year educ seutukunta16)

save "W:\intimate\data\cross_dv_aizer.dta", replace 
restore 


use `extra2006', clear
forvalues i = 2007/2016 {
    append using `extra`i''
}

// collapse the data into educ year and region cell 
rename year_event year 
gen dv=1 

destring ututku, replace 
g level = floor(ututku/100000)
replace level = 0 if missing(ututku)

gen educ=1 if level<3
replace educ=2 if leve>=3 & level<=5
replace educ=3 if level>=6

gen age=floor(plaintiff_age/5)

collapse (sum) dv, by(year educ age seutukunta16)

save "W:\intimate\data\cross_dv.dta", replace

use "W:\intimate\data\cross_dv.dta", replace
merge 1:m year educ age seutukunta16 using "$data_folder\munic_edu_age_gender_oo", keep(master match) nogen 

drop if male==. 
reshape wide dv ptoim1 tyotu obs emp ann_earn, i(seutukunta16 educ age year) j(male)

gen log_dv=log(dv0)

destring seutukunta16, replace 

* time trends 
tab seutukunta16, gen (region)

forvalues i=1/70{
   gen year_region`i'=year*region`i'
}



sort seutukunta16 educ year 
bysort seutukunta16 educ: gen log_dv_lag=log_dv[_n-1]


* simplified specification 
rename ptoim10 women_oo
rename ptoim11 men_oo

gen oo_emp=1 

save "$data_folder\oo_emp_dv", replace 

// IV for replication

local var_list ptoim1 tyotu ika ututku sukup seutukunta16 tyopv toimiala 
forvalues i = 2000/2016 {
use `var_list' using "D:\ready-made\FLEED_TOTAL\2016\fleed_kokonais_`i'.dta", clear

destring ututku, replace 
g level = floor(ututku/100000)
replace level= 0 if missing(ututku)

gen educ=1 if level<3
replace educ=2 if leve>=3 & level<=5
replace educ=3 if level>=6

	
g male = (sukup=="1")
keep if ika>=20
g age = floor(ika/5)

g day_wage = tyotu/tyopv

destring ptoim1, replace
replace ptoim1 = (ptoim1==11)

*INDUSTRY AND OCCUPATION FOR EWCS DATA
	gen nace_2=real(substr(toimiala,1,2))

	loc vars "nace"
	foreach var in `vars' {
		g `var'=.
		replace `var'=1 if `var'_2==1 | `var'_2==2 | `var'_2==5
		replace `var'=2 if `var'_2>=10 & `var'_2<=37
		replace `var'=3 if `var'_2==40 | `var'_2==41
		replace `var'=4 if `var'_2==45  
		replace `var'=5 if `var'_2>=50 & `var'_2<=52
		replace `var'=6 if `var'_2==55  

	}

	drop nace_2 
	label var nace "Industry"

g emp = ptoim1 & tyotu>0 & tyotu~=.
gen ann_earn= tyotu
*keep if tyotu~=.

g obs = 1 
collapse (mean) ptoim1 tyotu day_wage (sum) obs emp ann_earn, by(educ male nace seutukunta16)

save "$data_folder\aizer_munic_ed_oo_`i'", replace 

}

// take 2000 as base year
use "$data_folder\aizer_munic_ed_oo_2000", clear
replace nace=0 if nace==.
sort sort seutukunta16 educ male nace
bys seutukunta16 educ male: egen denom=total(emp)
g gamma = emp/denom
replace gamme=0 if gamma==.
save "$data_folder\aizer_munic_ed_oo_share", replace

// calculate shift
use "$data_folder\aizer_munic_ed_oo_2000", clear
g year=2000
forvalues i = 2000/2016 {
	append using "$data_folder\aizer_munic_ed_oo_`i'"
	replace year=`i' if missing(year)
}
replace nace =0 if nace==.
merge m:1 year using "$data_folder\cpi"
replace ann_earn=ann_earn/cpi
drop if _merge==2
drop _merge

collapse (sum) ann_earn emp, by(educ year nace seutukunta16)

bys year educ nace: egen ann_earn_other = total(ann_earn)
bys year educ nace: egen emp_other = total(emp)
gen tyotu_shift = (ann_earn_other - ann_earn)/(emp_other-emp)
drop *_other ann_earn emp
replace tyotu_shift=0 if tyotu_shift==.
save "$data_folder\aizer_munic_ed_oo_shift", replace

use "$data_folder\aizer_munic_ed_oo_share", clear
expand 17
bys seutukunta16 educ male nace: gen year = 1999 + _n

merge m:1 seutukunta16 year nace educ using "$data_folder\aizer_munic_ed_oo_shift"
drop _merge
drop denom ptoim1 tyotu day_wage obs emp ann_earn

reshape wide gamma tyotu_shift, i(year seutukunta16 educ male) j(nace)

forvalues i =0/6 {
	replace tyotu_shift`i'=0 if tyotu_shift`i'==.
	replace gamma`i'=0 if gamma`i'==.
}

gen wage_op = tyotu_shift0*gamma0 + tyotu_shift1*gamma1 + tyotu_shift2*gamma2 ///
			+ tyotu_shift3*gamma3 + tyotu_shift4*gamma4 + tyotu_shift5*gamma5 ///
			+ tyotu_shift6*gamma6
			
save "$data_folder\aizer_munic_ed_oo_bartik_iv", replace





















